<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="数据结构,算法,二叉搜索树,">





  <link rel="alternate" href="/blog/atom.xml" title="编程那点事" type="application/atom+xml">






<meta name="description" content="学习JavaScript数据结构与算法（一）：栈与队列学习JavaScript数据结构与算法（二）：链表学习JavaScript数据结构与算法（三）：集合学习JavaScript数据结构与算法（四）：二叉搜索树  我与二叉树的前尘往事在刚学编程时，就知道有一种数据结构叫“树”，树中的翘楚是“二叉树”，“红黑树”等。据说“树”构在编程界呼风唤雨无所不能。让无数程序员闻风丧胆。甚至在面试时，更是有“">
<meta name="keywords" content="数据结构,算法,二叉搜索树">
<meta property="og:type" content="article">
<meta property="og:title" content="学习JavaScript数据结构与算法（四）：二叉搜索树">
<meta property="og:url" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/index.html">
<meta property="og:site_name" content="编程那点事">
<meta property="og:description" content="学习JavaScript数据结构与算法（一）：栈与队列学习JavaScript数据结构与算法（二）：链表学习JavaScript数据结构与算法（三）：集合学习JavaScript数据结构与算法（四）：二叉搜索树  我与二叉树的前尘往事在刚学编程时，就知道有一种数据结构叫“树”，树中的翘楚是“二叉树”，“红黑树”等。据说“树”构在编程界呼风唤雨无所不能。让无数程序员闻风丧胆。甚至在面试时，更是有“">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree1.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree.png">
<meta property="og:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree2.png">
<meta property="og:updated_time" content="2019-10-03T05:53:45.951Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="学习JavaScript数据结构与算法（四）：二叉搜索树">
<meta name="twitter:description" content="学习JavaScript数据结构与算法（一）：栈与队列学习JavaScript数据结构与算法（二）：链表学习JavaScript数据结构与算法（三）：集合学习JavaScript数据结构与算法（四）：二叉搜索树  我与二叉树的前尘往事在刚学编程时，就知道有一种数据结构叫“树”，树中的翘楚是“二叉树”，“红黑树”等。据说“树”构在编程界呼风唤雨无所不能。让无数程序员闻风丧胆。甚至在面试时，更是有“">
<meta name="twitter:image" content="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lhweb.gitee.io/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/">





  <title>学习JavaScript数据结构与算法（四）：二叉搜索树 | 编程那点事</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">编程那点事</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lhweb.gitee.io/blog/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lhweb">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="编程那点事">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">学习JavaScript数据结构与算法（四）：二叉搜索树</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-09-23T20:15:13+08:00">
                2018-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/javascript/" itemprop="url" rel="index">
                    <span itemprop="name">javascript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9A%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">学习JavaScript数据结构与算法（一）：栈与队列</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9A%E9%93%BE%E8%A1%A8/">学习JavaScript数据结构与算法（二）：链表</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E4%B8%89%EF%BC%89%EF%BC%9A%E9%9B%86%E5%90%88/">学习JavaScript数据结构与算法（三）：集合</a><br><a href="https://lhweb.gitee.io/blog/2018/09/23/%E5%AD%A6%E4%B9%A0JavaScript%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9A%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/">学习JavaScript数据结构与算法（四）：二叉搜索树</a></p>
</blockquote>
<h1 id="我与二叉树的前尘往事"><a href="#我与二叉树的前尘往事" class="headerlink" title="我与二叉树的前尘往事"></a>我与二叉树的前尘往事</h1><p>在刚学编程时，就知道有一种数据结构叫“树”，树中的翘楚是“二叉树”，“红黑树”等。<br>据说“树”构在编程界呼风唤雨无所不能。让无数程序员闻风丧胆。甚至在面试时，更是有“手写二叉树”，“翻转二叉树”等题目坐镇。</p>
<p>好吧，我承认这些在当时都把我吓住了。</p>
<p>但是当我颤抖着打开《学习JavaScript数据结构与算法》，开始敲下关于“树”的代码时，突然觉得，好像也没有那么难呢。<br>于是心怀激动，一口气敲完了书上的例子，中途也思考了很久，不断的在纸上演算等。但总的来说，还是学的很开心的。</p>
<h1 id="树の简介"><a href="#树の简介" class="headerlink" title="树の简介"></a>树の简介</h1><p>之前学的栈、队列、链表等数据结构，都是顺序数据结构。而树，将会是我们学的第一种非顺序数据结构。</p>
<p>放在现实里呢，有个很生动的例子，公司组织架构图。长这样:<br><img src="/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree1.png" alt="公司组织架构图"></p>
<p>而我们要学的树，长这样:<br><img src="/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree.png" alt="树の图示"></p>
<h2 id="节点简介"><a href="#节点简介" class="headerlink" title="节点简介"></a>节点简介</h2><p>其中，树中的每个元素，都叫做节点。从节点延伸而下的，叫子节点。<br>树顶部的节点叫根节点。每棵树只有一个根节点。（图中15就是根节点）<br>在节点中，有子节点的节点也称为内部节点，没有的话则被称为外部节点或者叶节点。<br>同时在节点中是有祖先和后代关系的，比如节点9的祖先就有13,7,6,15四个。</p>
<h2 id="节点属性"><a href="#节点属性" class="headerlink" title="节点属性"></a>节点属性</h2><p>深度: 节点的深度取决于其祖先的数量，节点9的深度就是4。<br>树的高度，树的高度体现为节点深度的最大值。<br>比如上图，节点深度最大值为4，则树的高度为4。</p>
<h2 id="二叉树与二叉搜索树"><a href="#二叉树与二叉搜索树" class="headerlink" title="二叉树与二叉搜索树"></a>二叉树与二叉搜索树</h2><p>二叉树的最大特点就在于，它的节点最多只有两个子节点:左侧子节点和右侧子节点。<br>二叉搜索树则是二叉树的一种，但它只允许你在左侧节点储存比父节点小的值，右侧只允许储存比父节点大的值。<br>像刚才的这幅图，就是二叉搜索树。<br><img src="/blog/2018/09/23/学习JavaScript数据结构与算法（四）：二叉搜索树/tree2.png" alt="二叉搜索树"><br>而我们本文要学习的内容，就是如何写一个二叉搜索树。</p>
<h2 id="JavaScipt中二叉搜索树的实现"><a href="#JavaScipt中二叉搜索树的实现" class="headerlink" title="JavaScipt中二叉搜索树的实现"></a>JavaScipt中二叉搜索树的实现</h2><p>首先，创建一个构造函数。<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二叉搜索树的构造函数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">BinarySearchTree</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 二叉搜索树键的构造函数</span></span><br><span class="line"><span class="comment">   * @param &#123;Number&#125; key 要生成的键值</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">var</span> Node = <span class="function"><span class="keyword">function</span><span class="params">(key)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 键值</span></span><br><span class="line">    <span class="keyword">this</span>.key = key;</span><br><span class="line">    <span class="comment">// 左子节点</span></span><br><span class="line">    <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="comment">// 右子节点</span></span><br><span class="line">    <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 二叉树的根节点，不存在时表示为Null</span></span><br><span class="line"><span class="comment">   * @type &#123;Null or Number&#125;</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">var</span> root = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在之前提到过的双向链表中，每个节点包含两个指针，一个指向左侧节点，一个指向右侧节点。在二叉搜索树中，每个节点也有两个指针，一个指向左侧子节点，一个指向右侧子节点。但在二叉搜索树中，我们把节点成为键，这是术语。</p>
<p>二叉搜索树需要有如下的方法:</p>
<ul>
<li><p>insert(key): 向树中插入一个新的键</p>
</li>
<li><p>inOrderTraverse(): 通过中序遍历方式，遍历所有节点</p>
</li>
<li><p>preOrderTranverse(): 通过先序遍历方式，遍历所有节点</p>
</li>
<li><p>postOrderTranverse(): 通过后序遍历方式，遍历所有节点</p>
</li>
<li><p>min(): 返回树中最小的值</p>
</li>
<li><p>max(): 返回树中最大的值</p>
</li>
<li><p>search(key): 搜索某个值，在树中则返回true</p>
</li>
<li><p>remove(key): 从树中移除某个键</p>
</li>
</ul>
<p>二叉搜索树的实现，基本都与递归有关（对我来说递归很绕，花了很久才理解）。如果不清楚递归相关概念，可以看看下面的参考链接。</p>
<p><a href="https://www.zhihu.com/question/20507130" target="_blank" rel="noopener">什么是递归</a></p>
<h3 id="insert方法"><a href="#insert方法" class="headerlink" title="insert方法:"></a>insert方法:</h3><p>说明:向树中插入一个新的键<br>实现:<br><figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 插入某个键到二叉树中</span></span><br><span class="line"><span class="comment"> * @param  &#123;Number&#125; key 要插入的键值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="built_in">this</span>.insert = <span class="function"><span class="keyword">function</span></span>(key) &#123;</span><br><span class="line">  <span class="comment">// 用传入的值生成二叉树的键</span></span><br><span class="line">  <span class="keyword">var</span> <span class="keyword">new</span><span class="type">Node</span> = <span class="keyword">new</span> <span class="type">Node</span>(key);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 根节点为Null时，传入的键则为根节点</span></span><br><span class="line">  <span class="comment">// 否则调用insertNode函数来插入子节点</span></span><br><span class="line">  <span class="keyword">if</span> (root === <span class="literal">null</span>) &#123;</span><br><span class="line">    root = <span class="keyword">new</span><span class="type">Node</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    insertNode(root, <span class="keyword">new</span><span class="type">Node</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 用于插入子节点。</span></span><br><span class="line"><span class="comment"> * @param  &#123;Node&#125; node    根节点</span></span><br><span class="line"><span class="comment"> * @param  &#123;Node&#125; newNode 要插入的节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> insertNode = <span class="function"><span class="keyword">function</span></span>(node, <span class="keyword">new</span><span class="type">Node</span>) &#123;</span><br><span class="line">  <span class="comment">//由于二叉搜索树的性质，所以当键值小于当前所在节点的键值</span></span><br><span class="line">  <span class="comment">//则使得左子结点成为新的要比较的节点，进行递归调用</span></span><br><span class="line">  <span class="comment">//如果左子结点为null，则将键值赋值给左子结点。</span></span><br><span class="line">  <span class="comment">//如果键值大于当前所在节点的键值，原理同上。</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">new</span><span class="type">Node</span>.key &lt; node.key) &#123;</span><br><span class="line">    <span class="keyword">if</span> (node.left === <span class="literal">null</span>) &#123;</span><br><span class="line">      node.left = <span class="keyword">new</span><span class="type">Node</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      insertNode(node.left, <span class="keyword">new</span><span class="type">Node</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (node.right === <span class="literal">null</span>) &#123;</span><br><span class="line">      node.right = <span class="keyword">new</span><span class="type">Node</span></span><br><span class="line"><span class="type"></span>    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      insertNode(node.right, <span class="keyword">new</span><span class="type">Node</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="inOrderTraverse方法"><a href="#inOrderTraverse方法" class="headerlink" title="inOrderTraverse方法:"></a>inOrderTraverse方法:</h3><p>说明:通过中序遍历方式，遍历所有节点<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 中序遍历操作，常用于排序。会把树中元素从小到大的打印出来。</span><br><span class="line"> * 因为在javascript的递归中，遇到递归是，会优先调用递归的函数。直到递归不再进行。</span><br><span class="line"> * 然后会在递归调用的最后一个函数中执行其它语句。再一层层的升上去。</span><br><span class="line"> * 所以中序遍历会有从小到大的输出结果。</span><br><span class="line"> * 后续的先序和后续遍历和这个原理差不多，取决于callback放在哪儿。</span><br><span class="line"> * </span><br><span class="line"> * @param  &#123;Function&#125; callback 获取到节点后的回调函数</span><br><span class="line"> */</span><br><span class="line">this.inOrderTraverse = function(callback) &#123;</span><br><span class="line">  inOrderTraverseNode(root, callback);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 中序遍历的辅助函数，用于遍历节点</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125;   node</span>     遍历开始的节点，默认为root</span><br><span class="line"> * @param  &#123;Function&#125; callback 获取到节点后的回调函数</span><br><span class="line"> * @return &#123;[<span class="keyword">type</span>]&#125;            [description]</span><br><span class="line"> */</span><br><span class="line">var inOrderTraverseNode = function(<span class="keyword">node</span><span class="title">, callback</span>) &#123;</span><br><span class="line">  // 当前节点不为NULL则继续递归调用</span><br><span class="line">  if (<span class="keyword">node</span> <span class="title">!= null</span>) &#123;</span><br><span class="line">    inOrderTraverseNode(<span class="keyword">node</span>.<span class="title">left</span>, callback);</span><br><span class="line">    // 获取到节点后，调用的函数</span><br><span class="line">    callback(<span class="keyword">node</span>.<span class="title">key</span>);</span><br><span class="line">    inOrderTraverseNode(<span class="keyword">node</span>.<span class="title">right</span>, callback);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<p>假如我们这儿加入打印节点值的函数:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> printNode = <span class="function"><span class="keyword">function</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">inOrderTraverse(printNode) <span class="comment">// 输出排序后树的值</span></span><br></pre></td></tr></table></figure></p>
<h3 id="preOrderTranverse方法"><a href="#preOrderTranverse方法" class="headerlink" title="preOrderTranverse方法:"></a>preOrderTranverse方法:</h3><p>说明:通过先序遍历方式，遍历所有节点<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 前序遍历操作，常用于打印一个结构化的文档</span><br><span class="line"> * @param  &#123;Function&#125; callback 获取到节点后的回调函数</span><br><span class="line"> */</span><br><span class="line">this.preOrderTranverse = function(callback) &#123;</span><br><span class="line">  preOrderTranverseNode(root, callback);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 前序遍历的辅助函数，用于遍历节点</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125;   node</span>     遍历开始的节点，默认为root</span><br><span class="line"> * @param  &#123;Function&#125; callback 获取到节点后的回调函数</span><br><span class="line"> */</span><br><span class="line">var preOrderTranverseNode = function(<span class="keyword">node</span><span class="title">, callback</span>) &#123;</span><br><span class="line">  if (<span class="keyword">node</span> <span class="title">!= null</span>) &#123;</span><br><span class="line">    callback(<span class="keyword">node</span>.<span class="title">key</span>);</span><br><span class="line">    preOrderTranverseNode(<span class="keyword">node</span>.<span class="title">left</span>, callback);</span><br><span class="line">    preOrderTranverseNode(<span class="keyword">node</span>.<span class="title">right</span>, callback);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="postOrderTranverse方法"><a href="#postOrderTranverse方法" class="headerlink" title="postOrderTranverse方法:"></a>postOrderTranverse方法:</h3><p>说明:通过后序遍历方式，遍历所有节点<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 后序遍历操作，常用于计算所占空间</span><br><span class="line"> * @param  &#123;Function&#125; callback 获取到节点后的回调函数</span><br><span class="line"> */</span><br><span class="line">this.postOrderTranverse = function(callback) &#123;</span><br><span class="line">  postOrderTranverseNode(root, callback);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 后序遍历的辅助函数，用于遍历节点</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125;   node</span>     遍历开始的节点，默认为root</span><br><span class="line"> * @param  &#123;Function&#125; callback 获取到节点后的回调函数</span><br><span class="line"> */</span><br><span class="line">var postOrderTranverseNode = function(<span class="keyword">node</span><span class="title">, callback</span>) &#123;</span><br><span class="line">  postOrderTranverseNode(<span class="keyword">node</span>.<span class="title">left</span>, callback);</span><br><span class="line">  postOrderTranverseNode(<span class="keyword">node</span>.<span class="title">right</span>, callback);</span><br><span class="line">  callback(<span class="keyword">node</span>.<span class="title">key</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="min方法"><a href="#min方法" class="headerlink" title="min方法:"></a>min方法:</h3><p>说明:返回树中最小的值，由二叉搜索树的性质易知，最左侧的为最小值。则只需取得最左侧的值即可。<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 返回树中最小的值</span><br><span class="line"> * @return &#123;Function&#125; min函数的辅助函数</span><br><span class="line"> */</span><br><span class="line">this.min = function() &#123;</span><br><span class="line">  return minNode(root);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * min函数的辅助函数</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125; node</span> 查找开始的节点，默认为root</span><br><span class="line"> */</span><br><span class="line">var minNode = function(<span class="keyword">node</span><span class="title">) &#123;</span></span><br><span class="line"><span class="title">  // 如果node</span>存在，则开始搜索。能避免树的根节点为Null的情况</span><br><span class="line">  if (<span class="keyword">node</span><span class="title">) &#123;</span></span><br><span class="line"><span class="title">    // 只要树的左侧子节点不为null</span>，则把左子节点赋值给当前节点。</span><br><span class="line">    // 若左子节点为null，则该节点肯定为最小值。</span><br><span class="line">    while (<span class="keyword">node</span> <span class="title">&amp;&amp; node</span>.left !== null) &#123;</span><br><span class="line">      <span class="keyword">node</span> <span class="title">= node</span>.left;</span><br><span class="line">    &#125;</span><br><span class="line">    return <span class="keyword">node</span>.<span class="title">key</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  return null;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="max方法"><a href="#max方法" class="headerlink" title="max方法:"></a>max方法:</h3><p>说明:返回树中最大的值，由min函数易知，最大值在最右侧。<br>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 返回树中最大的值</span><br><span class="line"> * @return &#123;Function&#125; max函数的辅助函数</span><br><span class="line"> */</span><br><span class="line">this.max = function() &#123;</span><br><span class="line">  return maxNode(root);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * max函数的辅助函数</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125; node</span> 查找开始的节点，默认为root</span><br><span class="line"> * @return &#123;Key&#125;      节点的值</span><br><span class="line"> */</span><br><span class="line">var maxNode = function(<span class="keyword">node</span><span class="title">) &#123;</span></span><br><span class="line"><span class="title">  if</span> (<span class="keyword">node</span><span class="title">) &#123;</span></span><br><span class="line"><span class="title">    while</span> (<span class="keyword">node</span> <span class="title">&amp;&amp; node</span>.right !== null) &#123;</span><br><span class="line">      <span class="keyword">node</span> <span class="title">= node</span>.right;</span><br><span class="line">    &#125;</span><br><span class="line">    return <span class="keyword">node</span>.<span class="title">key</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  return null;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="search方法"><a href="#search方法" class="headerlink" title="search方法:"></a>search方法:</h3><p>说明: 搜索某个值，在树中则返回true<br>实现:<br><figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 搜索某个值是否存在于树中</span><br><span class="line"> * @param  &#123;Node&#125;<span class="built_in"> key</span> 搜索开始的节点，默认<span class="built_in">为root</span></span><br><span class="line"> * @<span class="keyword">return</span> &#123;Function&#125;     search函数的辅助函数</span><br><span class="line"> */</span><br><span class="line">this.search = <span class="keyword">function</span><span class="built_in">(key</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> searchNode<span class="built_in">(root</span>,<span class="built_in"> key</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * search函数的辅助函数</span><br><span class="line"> * @param  &#123;Node&#125; <span class="type">node</span> 搜索开始的节点，默认<span class="built_in">为root</span></span><br><span class="line"> * @param  &#123;Key&#125;<span class="built_in"> key</span>  要搜索的键值</span><br><span class="line"> * @<span class="keyword">return</span> &#123;Boolean&#125;      找到节点则返<span class="built_in">回true</span>，否则返<span class="built_in">回false</span></span><br><span class="line"> */</span><br><span class="line">var searchNode = <span class="keyword">function</span>(<span class="type">node</span>,<span class="built_in"> key</span>) &#123;</span><br><span class="line">  // 如果根节点不存在，则直接返回null</span><br><span class="line">  <span class="keyword">if</span> (<span class="type">node</span> === null) &#123;</span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> false</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> <span class="built_in">(key</span> &lt; <span class="type">node</span><span class="built_in">.key</span>) &#123;</span><br><span class="line">    searchNode(<span class="type">node</span>.left,<span class="built_in"> key</span>)</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> <span class="built_in">(key</span> &gt; <span class="type">node</span><span class="built_in">.key</span>) &#123;</span><br><span class="line">    searchNode(<span class="type">node</span>.right,<span class="built_in"> key</span>)</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    // 如果该节点值等于传入的值，返<span class="built_in">回true</span></span><br><span class="line">    <span class="keyword">return</span><span class="built_in"> true</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="remove方法"><a href="#remove方法" class="headerlink" title="remove方法:"></a>remove方法:</h3><p>说明:从树中移除某个键，要应对的场景:</p>
<p>只是一个叶节点</p>
<p>有一个子节点</p>
<p>有两个子节点的节点<br>因为要应付不同的场景，所以这是最麻烦的方法了。让我思考了好久才理解。如果你觉得看不懂的话，可以下载源代码把这一段写一遍。</p>
<p>实现:<br><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 从树中移除某个键</span><br><span class="line"> * @param  &#123;Key&#125; key 要移除的键值</span><br><span class="line"> * @return &#123;Function&#125;     remove函数的辅助函数</span><br><span class="line"> */</span><br><span class="line">this.remove = function(key) &#123;</span><br><span class="line">  root = removeNode(root, key);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * remove函数的辅助函数</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125; node</span> 搜索开始的节点，默认为root</span><br><span class="line"> * @param  &#123;Key&#125; key   要移除的键值</span><br><span class="line"> * @return &#123;Boolean&#125;   移除成功则返回<span class="literal">true</span>，否则返回<span class="literal">false</span></span><br><span class="line"> */</span><br><span class="line">var removeNode = function(<span class="keyword">node</span><span class="title">, key</span>) &#123;</span><br><span class="line">  // 如果根节点不存在，则直接返回null</span><br><span class="line">  if (<span class="keyword">node</span> <span class="title">=== root</span>) &#123;</span><br><span class="line">    return null;</span><br><span class="line">  &#125;</span><br><span class="line">  // 未找到节点前，继续递归调用。</span><br><span class="line">  if (key <span class="tag">&lt; node.key) &#123;</span></span><br><span class="line"><span class="tag">    node.left = removeNode(node.left, key)</span></span><br><span class="line"><span class="tag">    return node;</span></span><br><span class="line"><span class="tag">  &#125; else if (key &gt;</span> <span class="keyword">node</span>.<span class="title">key</span>) &#123;</span><br><span class="line">    <span class="keyword">node</span>.<span class="title">right</span> = removeNode(<span class="keyword">node</span>.<span class="title">right</span>, key)</span><br><span class="line">    return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125; else</span> &#123;</span><br><span class="line">    // 第一种场景：只是一个叶节点</span><br><span class="line">    // 这种情况只需要直接把节点赋值为null即可</span><br><span class="line">    if (<span class="keyword">node</span>.<span class="title">left</span> === null &amp;&amp; <span class="keyword">node</span>.<span class="title">right</span> === null) &#123;</span><br><span class="line">      <span class="keyword">node</span> <span class="title">= null</span>;</span><br><span class="line">      // 处理完直接return节点</span><br><span class="line">      return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">    &#125;</span></span><br><span class="line"><span class="title">    // 第二种场景：有一个子节点</span></span><br><span class="line"><span class="title">    // 如果左节点为null</span>，则代表右节点存在。</span><br><span class="line">    // 于是把当前节点赋值为存在的那个子节点</span><br><span class="line">    if (<span class="keyword">node</span>.<span class="title">left</span> === null) &#123;</span><br><span class="line">      <span class="keyword">node</span> <span class="title">= node</span>.right;</span><br><span class="line">      // 处理完直接return节点</span><br><span class="line">      return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">    &#125; else</span> if (<span class="keyword">node</span>.<span class="title">right</span> == null) &#123;</span><br><span class="line">      <span class="keyword">node</span> <span class="title">= node</span>.left;</span><br><span class="line">      // 处理完直接return节点</span><br><span class="line">      return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">    &#125;</span></span><br><span class="line"><span class="title">    // 第三种场景：有两个子节点</span></span><br><span class="line"><span class="title">    // 首先加入辅助节点，同时找寻右子节点中的最小节点</span></span><br><span class="line"><span class="title">    // 并把当前节点替换为右子节点中的最小节点</span></span><br><span class="line"><span class="title">    // 同时为了避免节点重复，移除右子节点中的最小节点</span></span><br><span class="line"><span class="title">    var</span> aux = findMinNode(<span class="keyword">node</span>.<span class="title">right</span>);</span><br><span class="line">    <span class="keyword">node</span>.<span class="title">key</span> = aux.key;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">node</span>.<span class="title">right</span> = removeNode(<span class="keyword">node</span>.<span class="title">right</span>, aux.key);</span><br><span class="line">    // 处理完直接return节点</span><br><span class="line">    return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125;</span></span><br><span class="line"><span class="title">&#125;;</span></span><br><span class="line"><span class="title"></span></span><br><span class="line"><span class="title">/**</span></span><br><span class="line"><span class="title"> * remove</span>函数的辅助函数</span><br><span class="line"> * @param  &#123;<span class="keyword">Node</span><span class="title">&#125; node</span> 查找开始的节点，默认为root</span><br><span class="line"> * @return &#123;<span class="keyword">Node</span><span class="title">&#125;      最小的节点</span></span><br><span class="line"><span class="title"> */</span></span><br><span class="line"><span class="title">var</span> findMinNode = function(<span class="keyword">node</span><span class="title">) &#123;</span></span><br><span class="line"><span class="title">  // 如果node</span>存在，则开始搜索。能避免树的根节点为Null的情况</span><br><span class="line">  if (<span class="keyword">node</span><span class="title">) &#123;</span></span><br><span class="line"><span class="title">    // 只要树的左侧子节点不为null</span>，则把左子节点赋值给当前节点。</span><br><span class="line">    // 若左子节点为null，则该节点肯定为最小值。</span><br><span class="line">    while (<span class="keyword">node</span> <span class="title">&amp;&amp; node</span>.left !== null) &#123;</span><br><span class="line">      <span class="keyword">node</span> <span class="title">= node</span>.left;</span><br><span class="line">    &#125;</span><br><span class="line">    return <span class="keyword">node</span><span class="title">;</span></span><br><span class="line"><span class="title">  &#125;</span></span><br><span class="line"><span class="title">  return</span> null;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/数据结构/" rel="tag"># 数据结构</a>
          
            <a href="/blog/tags/算法/" rel="tag"># 算法</a>
          
            <a href="/blog/tags/二叉搜索树/" rel="tag"># 二叉搜索树</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/23/学习JavaScript数据结构与算法（三）：集合/" rel="next" title="学习JavaScript数据结构与算法（三）：集合">
                <i class="fa fa-chevron-left"></i> 学习JavaScript数据结构与算法（三）：集合
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/23/TypeScript-学习笔记-之-枚举与类型推断/" rel="prev" title="TypeScript 学习笔记 之 枚举与类型推断">
                TypeScript 学习笔记 之 枚举与类型推断 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">lhweb</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#我与二叉树的前尘往事"><span class="nav-number">1.</span> <span class="nav-text">我与二叉树的前尘往事</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#树の简介"><span class="nav-number">2.</span> <span class="nav-text">树の简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#节点简介"><span class="nav-number">2.1.</span> <span class="nav-text">节点简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#节点属性"><span class="nav-number">2.2.</span> <span class="nav-text">节点属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二叉树与二叉搜索树"><span class="nav-number">2.3.</span> <span class="nav-text">二叉树与二叉搜索树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScipt中二叉搜索树的实现"><span class="nav-number">2.4.</span> <span class="nav-text">JavaScipt中二叉搜索树的实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#insert方法"><span class="nav-number">2.4.1.</span> <span class="nav-text">insert方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#inOrderTraverse方法"><span class="nav-number">2.4.2.</span> <span class="nav-text">inOrderTraverse方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#preOrderTranverse方法"><span class="nav-number">2.4.3.</span> <span class="nav-text">preOrderTranverse方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#postOrderTranverse方法"><span class="nav-number">2.4.4.</span> <span class="nav-text">postOrderTranverse方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#min方法"><span class="nav-number">2.4.5.</span> <span class="nav-text">min方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#max方法"><span class="nav-number">2.4.6.</span> <span class="nav-text">max方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#search方法"><span class="nav-number">2.4.7.</span> <span class="nav-text">search方法:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#remove方法"><span class="nav-number">2.4.8.</span> <span class="nav-text">remove方法:</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lhweb</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
